#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
typedef long long LL;
typedef pair<LL, int> PII;
#define x first
#define y second
const int N = 1e5 + 10, M = 2 * N;
int h[N], ne[M], e[M], w[M], idx;
int ans[N];
int path[N];
LL dis[N];
int n, m;
bool st[N];
void add(int a, int b, int c)
{
	e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}

bool dij()
{
	for (int i = 1; i <= n; i++) {
		dis[i] = 1e15;
	}
	priority_queue<PII, vector<PII>, greater<PII>>heap;
	dis[1] = 0;
	//ans1[1] = 1;
	//ans[1].push_back(1);
	heap.push({ 0,1 });
	while (heap.size()) {
		PII t = heap.top();
		heap.pop();
		LL distance = t.x;
		int ver = t.y;
		if (st[ver])continue;
		st[ver] = 1;
		for (int i = h[ver]; ~i; i = ne[i]) {
			int j = e[i];
			if (!st[j] && dis[j] > distance + w[i]) {
				dis[j] = distance + w[i];
				ans[j] = ver;
				/*for (int x : ans[ver]) {
					ans[j].push_back(x);
				}
				ans[j].push_back(ver);
				ans1[j] = ans1[ver] + 1;*/
				heap.push({ dis[j],j });
			}
		}
	}
	if (dis[n] == 1e15)return 0;
	return 1;
}
int main()
{
	cin >> n >> m;
	memset(h, -1, sizeof(h));
	while (m--) {
		int a, b, c;
		cin >> a >> b >> c;
		add(a, b, c);
		add(b, a, c);
	}
	if (!dij()) cout << -1 << endl;
	else {
		int now = n;
		int k = 0;
		while (now != 1) {
			path[++k] = now;
			now = ans[now];
		}
		//cout << "--- ";
		cout << 1 << " ";
		for (int i = k; i >= 1; i--) {
			cout << path[i] << " ";
		}
	}
	//else {
	//	//cout << dis[n] << endl;
	//	//cout << "===" << ans1[n] << endl;
	//	//for (int i = ans[n].size() - ans1[n] + 1; i < ans[n].size(); i++) {
	//	//	cout << ans[n][i] << " ";
	//	//}
	//	///*for (int x : ans[n]) {
	//	//	cout << x << " ";
	//	//}*/
	//	//cout << n << endl;
	//}
	return 0;
}